package com.hlhj.funlike_businessqzxd.api;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.example.mymvp.mvp.BaseMvpAty;
import com.example.mymvp.okrx.BaseAciton;
import com.example.mymvp.okrx.MyException;
import com.hlhj.funlike_businessqzxd.activity.BaseActivity;
import com.hlhj.funlike_businessqzxd.activity.LoginAty;
import com.hlhj.funlike_businessqzxd.data.SPUser;

import io.reactivex.disposables.Disposable;

public abstract class BaseObser<T> implements BaseAciton<T>{
    private Context context;

    public BaseObser(Context context) {
        this.context = context;
    }

    /*
        请求失败
         */
    @Override
    public void httpFailed(Throwable throwable) {
        if (throwable instanceof MyException){
            MyException exception = (MyException) throwable;
            if (exception.getCode()==201){
                Log.e("fhp","该去登录了");
                Toast.makeText(context,"登录已过期，重新登录",Toast.LENGTH_SHORT).show();
                if (context instanceof BaseActivity){
                    ((BaseActivity) context).finish();
                    context.startActivity(new Intent(context, LoginAty.class));
                    SPUser.getUser(context).setToken("");
                }
                //todo 去登陆
            }
            else if (exception.getCode()==300){
                //todo 。。。。
            }
            else if (exception.getCode()==0){
                Toast.makeText(context,exception.getMessage(),Toast.LENGTH_SHORT).show();
            }else if (exception.getCode()==2){

            }
            erroCode(exception);
        }

    }

    public  void erroCode(MyException e){

    }
    @Override
    public void onNext(T t) {
        try {
            DefaultImpls.onNext(this,t);
        }catch (Exception e){
            Log.e("zy","报错了"+e.getMessage());
            e.printStackTrace();
        }
    }

    @Override
    public void acheSuccess(T t) {
    }


    @Override
    public void onError(Throwable throwable) {
        DefaultImpls.onError(this,throwable);
    }

    @Override
    public void onSubscribe(Disposable d) {
        if (context instanceof BaseMvpAty){
            ((BaseMvpAty) context).addDisable(d);
        }
    }

    @Override
    public void onComplete() {

    }
}
